<?php
namespace frontend\controllers;

use common\models\Catalog;
use common\models\Config;
use common\models\Module;
use common\models\Post;
use common\utils\XCache;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;

/**
 * 前台基本控制器
 */
class BaseController extends Controller
{
    protected $seo = false;

    public function xrender($view, $params = [])
    {
        $conf = XCache::config();
        $mdu = XCache::modules();

        if ($this->seo === false) {
            $this->view->registerMetaTag(['name' => 'keywords', 'content' => $conf['site_keywords']]);
            $this->view->registerMetaTag(['name' => 'description', 'content' => $conf['site_description']]);
        } else {
            $this->view->registerMetaTag(['name' => 'keywords', 'content' => $this->seo['keywords']]);
            $this->view->registerMetaTag(['name' => 'description', 'content' => $this->seo['description']]);
        }

        if (!isset($this->view->title)) {
            $this->view->title = $conf['site_title'];
        } else {
            $this->view->title = $this->view->title . ' - ' . $conf['site_title'];
        }

        //把conf传给layout
        Yii::$app->view->params['conf'] = $conf;

        return $this->render($view, array_merge($params, ['conf' => $conf, 'mdu' => $mdu]));
    }

    /**
     * @param $message
     * @param string $type
     */
    protected function tips($message, $type = 'error')
    {
        echo $this->render('/site/tips', ['message' => $message, 'type' => $type]);
        exit();
    }

    /**
     * 设置SEO
     * @param $keywords
     * @param $description
     */
    protected function setSeo($keywords, $description)
    {
        $this->seo = ['keywords' => $keywords, 'description' => $description];
    }

    /**
     * 异步获取产品条目
     */
    public function actionAjaxProduct($alias){
        $this->layout=false;
        $num=Yii::$app->request->get("list",8);
        $paging=Yii::$app->request->get("paging",false);
        $data = Post::find()->with('attr')->select("id,title,author,intro,face");
        //获取$catalogId子类
        $catalog = Catalog::findOne(['alias' => $alias]);
        if (!$catalog) {
            return [];
        }
        $childIdes = $catalog->getChildIdes($catalog->id);
        if ($childIdes) {
            $ids = $catalog->id . ',' . $childIdes;
            $data->where("catalog_id in($ids)");
        } else {
            $data->where("catalog_id=$catalog->id");
        }
        $list = $data->orderBy("recommend desc,sort asc,id desc")->asArray();
        $result=$this->search($list,$num);
        $list=$result['models'];
        foreach ((array)$list as $key => $item) {
            $list[$key]['url'] = \yii\helpers\Url::to(['/post', 'id' => $item['id']]);
            //补充内容属性
            //$attres = PostAttr::find()->where("post_id=" . $item['id'])->asArray()->all();
            $attributes = [];
            foreach ((array)$item['attr'] as $attr) {
                $attributes[$attr['name']] = $attr['val'];
            }
            $list[$key]['attributes'] = $attributes;
        }
        $products=$this->render('/public/product-list',['items'=>$list,'page'=>$paging]);
        exit($products);
    }

    /**
     * 异步获取案例
     */
    public function actionAjaxCase($alias){
        $this->layout=false;
        $num=Yii::$app->request->get("list",8);
        $paging=Yii::$app->request->get("paging",false);
        $data = Post::find()->with('attr')->select("id,title,author,intro,face");
        //获取$catalogId子类
        $catalog = Catalog::findOne(['alias' => $alias]);
        if (!$catalog) {
            return [];
        }
        $childIdes = $catalog->getChildIdes($catalog->id);
        if ($childIdes) {
            $ids = $catalog->id . ',' . $childIdes;
            $data->where("catalog_id in($ids)");
        } else {
            $data->where("catalog_id=$catalog->id");
        }
        $list = $data->orderBy("recommend desc,sort asc,id desc")->asArray();
        $result=$this->search($list,$num);
        $list=$result['models'];
        foreach ((array)$list as $key => $item) {
            $list[$key]['url'] = \yii\helpers\Url::to(['/post', 'id' => $item['id']]);
            //补充内容属性
            //$attres = PostAttr::find()->where("post_id=" . $item['id'])->asArray()->all();
            $attributes = [];
            foreach ((array)$item['attr'] as $attr) {
                $attributes[$attr['name']] = $attr['val'];
            }
            $list[$key]['attributes'] = $attributes;
        }
        $products=$this->render('/public/case-list',['items'=>$list,'page'=>$paging]);
        exit($products);
    }
    /**
     * 异步获取新闻
     */
    public function actionAjaxNews($alias){
        $this->layout=false;
        $num=Yii::$app->request->get("list",8);
        $paging=Yii::$app->request->get("paging",false);
        $data = Post::find()->with('attr')->select("id,title,author,intro,face,created_at,updated_at,views");
        //获取$catalogId子类
        $catalog = Catalog::findOne(['alias' => $alias]);
        if (!$catalog) {
            return [];
        }
        $childIdes = $catalog->getChildIdes($catalog->id);
        if ($childIdes) {
            $ids = $catalog->id . ',' . $childIdes;
            $data->where("catalog_id in($ids)");
        } else {
            $data->where("catalog_id=$catalog->id");
        }
        $list = $data->orderBy("recommend desc,sort asc,id desc")->asArray();
        $result=$this->search($list,$num);
        $list=$result['models'];
        foreach ((array)$list as $key => $item) {
            $list[$key]['url'] = \yii\helpers\Url::to(['/post', 'id' => $item['id']]);
            //补充内容属性
            //$attres = PostAttr::find()->where("post_id=" . $item['id'])->asArray()->all();
            $attributes = [];
            foreach ((array)$item['attr'] as $attr) {
                $attributes[$attr['name']] = $attr['val'];
            }
            $list[$key]['attributes'] = $attributes;
        }
        $products=$this->render('/public/news-list',['items'=>$list,'page'=>$paging]);
        exit($products);
    }
    /**
     * 异步获取问题
     */
    public function actionAjaxQuestion($alias){
        $this->layout=false;
        $num=Yii::$app->request->get("list",8);
        $paging=Yii::$app->request->get("paging",false);
        $data = Post::find()->with('attr')->select("id,title,author,intro,face,created_at,updated_at,views");
        //获取$catalogId子类
        $catalog = Catalog::findOne(['alias' => $alias]);
        if (!$catalog) {
            return [];
        }
        $childIdes = $catalog->getChildIdes($catalog->id);
        if ($childIdes) {
            $ids = $catalog->id . ',' . $childIdes;
            $data->where("catalog_id in($ids)");
        } else {
            $data->where("catalog_id=$catalog->id");
        }
        $list = $data->orderBy("recommend desc,sort asc,id desc")->asArray();
        $result=$this->search($list,$num);
        $list=$result['models'];
        foreach ((array)$list as $key => $item) {
            $list[$key]['url'] = \yii\helpers\Url::to(['/post', 'id' => $item['id']]);
            //补充内容属性
            //$attres = PostAttr::find()->where("post_id=" . $item['id'])->asArray()->all();
            $attributes = [];
            foreach ((array)$item['attr'] as $attr) {
                $attributes[$attr['name']] = $attr['val'];
            }
            $list[$key]['attributes'] = $attributes;
        }
        $products=$this->render('/public/question-list',['items'=>$list,'page'=>$paging]);
        exit($products);
    }
    public function search($query,$pageSize=10){
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count,'defaultPageSize'=>$pageSize]);
        $result=array();
        $result['models']=$query->offset($pagination->offset)->limit($pagination->limit)->all();
        $result['pagination']=$pagination;
        return $result;
    }
}
